package com.java.springbootwatermark.service;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @author luheng
 * @create 2019/12/25
 */
@Service
public class ImageUploadService {

    /**
     * 功能：上传图片
     * @param file 文件
     * @param uploadPath 服务器上上传文件的路径
     * @param physicalUploadPath  服务器上上传文件的物理路径
     * @return 上传文件的 URL相对地址
     */
    public String uploadImage(MultipartFile file, String uploadPath, String physicalUploadPath) {
        String filePath = physicalUploadPath + file.getOriginalFilename();
        try {
            File targetFile = new File(filePath);
            //判断文件父目录是否存在
            if(!targetFile.getParentFile().exists()){
                targetFile.getParentFile().mkdir();
            }
            FileUtils.writeByteArrayToFile(targetFile, file.getBytes());
        } catch (IOException e) {

        }
        return uploadPath + file.getOriginalFilename();
    }
}
